﻿package org.fasex.displayer
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import org.fas.tween.FTween;
	import org.fasex.preloader.LightRaySimpleColor;
	
	public class ImageLoader1 extends Sprite
	{
		public var index:int = -1;
		public var tween:FTween;
		private var loading:LightRaySimpleColor;
		private var er_source:String;
		private var img:Loader;
		private var borderThickness:Number;
		private var w:Number = 80;
		private var h:Number = 100;
		private var text:TextField;
		public var page:String;
		private var er_realY:Number;
		public function ImageLoader1(_x:Number=0,_y:Number=0,_w:Number=80,_h:Number=100,_borderThickness:Number=3,_text:String='',_page:String='')
		{
			super();
			this.borderThickness = _borderThickness;
			this.tween = new FTween();
			this.tween.cbHalf = this.tweenHalfHandler;
			//construct
				this.img = new Loader();
			//
			this.loading = new LightRaySimpleColor();
			this.loading.rayColor = 0xFF00FF;
//			this.loading.showPercent = false;
			this.addChild(this.loading);
			//
			this.text = new TextField();
			var _tf:TextFormat = new TextFormat();
			_tf.color = 0xEEEEEE;
			_tf.size = 11;
			_tf.align = 'center';
			this.text.defaultTextFormat = _tf;
			this.text.text = _text;
			this.text.visible = false;
			this.page = _page;
			this.addChild(this.text);
			//draw backgroud
			this.x = _x;
			this.y = _y;
			this.width = _w; //only set value
			this.height = _h;
			//
		}
		private function getRealY():Number{
			return er_realY;
		}
		public function set realY(v:Number):void
		{
			er_realY = v;
			if(this.tween.isRun){
			}else{
				this.y = v;
			}
		}
		public function easeTo(_x:Number,_y:Number,_w:Number,_h:Number):void{
			this.realY = _y;
			this.tween.fixs(this,['x','y','width','height'],[_x,this.getRealY,_w,_h],null,30);
			this.text.visible = false;
			this.tween.start();
		}
		private function tweenHalfHandler(_v:Array=null):void{
			this.showText();
		}
		public function showText():void{
			if(this.index == 0){
				this.text.visible = true;
			}
		}
		override public function set width(value:Number) : void{
			this.w = value;
			this.drawBg();
			if(this.contains(this.img)){
				this.img.width = this.w-this.borderThickness*2;
				this.img.x = this.borderThickness;
			}
			if(this.contains(this.loading)){
				this.loading.x = this.w/2;
			}
			this.text.width = this.w;
		}
		override public function set height(value:Number) : void{
			this.h = value;
			this.drawBg();
			if(this.contains(this.img)){
				this.img.height = this.h-this.borderThickness*2;
				this.img.y = this.borderThickness;
			}
			if(this.contains(this.loading)){
				this.loading.y = this.h/2;
			}
			this.text.y = this.h;
		}
		public function get source():String
		{
			return er_source;
		}
		public function set source(v:String):void
		{
			er_source = v;
			this.img.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,evtImgLoadPro);
			this.img.contentLoaderInfo.addEventListener(Event.COMPLETE,evtImgLoaderC);
			this.img.load(new URLRequest(v));
//			this.addChild(this.img);
		}
		private function evtImgLoaderC(_e:Event):void{
			this.img.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,evtImgLoadPro);
			this.img.contentLoaderInfo.removeEventListener(Event.COMPLETE,evtImgLoaderC);
//			trace(this.img.width,this.img.height);
			this.img.x = this.borderThickness;
			this.img.y = this.borderThickness;
			this.img.width = this.w - this.borderThickness*2;
			this.img.height = this.h - this.borderThickness*2;
			this.addChild(this.img);
			this.removeChild(this.loading);
			this.loading.destory();
			//
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		private function evtImgLoadPro(_e:ProgressEvent):void{
			if(_e.bytesTotal>0){
				this.loading.percent = int(_e.bytesLoaded/_e.bytesTotal*100);
			}
		}
		private function drawBg():void{
			this.graphics.clear();
			this.graphics.beginFill(0xEEEEEE,1);
			this.graphics.drawRect(0,0,this.w,this.h);
			if(!this.contains(this.img)){
				this.graphics.beginFill(0x222222,1);
				this.graphics.drawRect(this.borderThickness,this.borderThickness,this.w-this.borderThickness*2,this.h-this.borderThickness*2);
			}
			this.graphics.endFill();
		}
	}
}
